var jsApiParameters = "";
var red_envelope_id = 0;
var flag = true;

layui.use(['layer'], function () {
    var layer = layui.layer;

    function initPage() {
        var token = sessionStorage.access_token;
        if(token==null){
            againToken();
            return;
        }
        var vue = new Vue({
            el: '#index_div',
            data: {
                on_flag:true,
                rules_list: {},  //挑战规则
                config_info: {},   //用户配置信息
            },
            watch: {},
            methods: {
                onInitiateChallenge: function () {

                    if(!flag){
                        return;
                    }
                    var rule = $("input[name='money']:checked");
                    var standard_id = parseInt(rule.val());
                    var is_visible = $("input[name='is_visible']:checked").val() == 1 ? 1 : 0;
                    var money = rule.data('money');
                    var data = {
                        token:token,
                        standard_id: standard_id,
                        is_visible: is_visible
                    }
                    //进行支付

                    if (!data.standard_id){
                        layer.msg('请选择挑战规则');
                        return;
                    }

                    flag = false;
                    $.ajax({
                        type: "post"
                        , url: '/index/pay/ajax_do_challenge'
                        , data: data
                        , async: false
                        , success: function (result) {
                            console.info(result);
                            if (result.code == -1) {
                                layer.msg(result.msg);
                                return;
                            }
                            if (result.code == 0) {
                                againToken();
                                return;
                            }
                            if (result.code == 1) {
                                jsApiParameters = result.data.jsApiParameters;
                                red_envelope_id = result.data.red_envelope_id;
                                jsApiParameters = JSON.parse(jsApiParameters);
                                docallpay();
                            }
                        }
                        , error: function (resq) {
                            console.info(resq);
                            layer.msg("服务器异常");
                        }
                    });

                }
            },
            filters: {
                integral: function (value) {
                    return parseInt(value);
                },
                getAnswerHref: function (val) {
                    return 'answer.html?id=' + val;
                },
                getIdStr: function (val) {
                    return 'money' + val;
                }
            }
        });

        /**
         * 初始化挑战规则
         */
        function initRulesList() {
            var data = {
                token: token
            }
            $.post("/index/challenge/ajax_challenge_rules", data, function (result) {
                //console.info(result);
                //if (result.code == -1) {   //失败
                //    layer.msg(result.msg);
                //}
                if (result.code == 0) { //Token失效
                    againToken();
                    return;
                }
                if (result.code == 1) { //成功
                    vue.rules_list = result.data;
                }
            });
        }

        initConfigInfo(vue, token);
        initRulesList();
    };

    /**
     * 初始化完成加载函数
     */
    $(function () {
        initGlobalPage();
        initPage();
    });

});


